<?php

namespace app\courier\controller;

use app\model\CourierInfo as ModelCourierInfo;
use app\model\CashStream as ModelCashStream;
use app\model\WithdrawLog as ModelWithdrawLog;
use think\Db;

#我的钱包
class MyWallet extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /*
     * 我的钱包首页
     * URI：/courier/myWallet.info
     */
    public function myWalletIndex()
    {
        $page = $this->_getContentParams('page');
        if (!$page) $page = 1;
        _toValidate('id', $page, 10005);
        $listRow = 20;

        #余额
        $balance = ModelCourierInfo::getInstance()->toValue(['id'=>$this->courier_id],'balance');
        #累积收入
        $where = [
            'map_id' => $this->courier_id,
            'map_type' => 1,
            'is_income' => 0,
        ];
        $sumInCome = ModelCashStream::getInstance()->toFind($where,'sum(money) as sumInCome');
        if ($sumInCome['sumInCome'] == null) $sumInCome['sumInCome'] = '0.00';
        #今日收入
        $where['gmt_create'] = array('between',[date('Y-m-d 00:00:00',time()),date('Y-m-d 23:59:59',time())]);
        $todayInCome = ModelCashStream::getInstance()->toFind($where,'sum(money) as todayIncome');
        if ($todayInCome['todayIncome'] == null) $todayInCome['todayIncome'] = '0.00';

        #流水列表
        unset($where['gmt_create']);
        $int_total_rows = ModelCashStream::getInstance()->toCount($where);//总条数
//        echo Db::name('cash_strem')->getLastSql();
        $int_total_pages = ceil($int_total_rows / $listRow);//总页数
        _toCompare(($page > $int_total_pages && $int_total_pages > 0), '==', true, 10007);
        $firstRow = ($page - 1) * $listRow;

        $cashList = ModelCashStream::getInstance()->toSelect($where,'id,money,describe,gmt_create,order_number,order_id as orderId',$firstRow,$listRow,'gmt_create desc');
//        echo Db::name('cash_stream')->getLastSql();
        foreach ($cashList as &$value)
        {
            $value['money'] = '+'.$value['money'];
        }
        #返回所有数据
        $data['balance'] = $balance;
        $data['sumInCome'] = $sumInCome['sumInCome'];
        $data['todayInCome'] = $todayInCome['todayIncome'];
        $data['totalPages'] = $int_total_pages;
        $data['cashList'] = $cashList;
        _returnData(['myWallet'=>$data]);

    }

    /*
     * 提现记录
     * URI：'courier/withdraw.log'
     */
    public function withdrawLog(){

        $page = $this->_getContentParams('page');
        $listRow = 20;
        if (!$page) $page = 1;
        _toValidate('id', $page, 10005);

        $where = [
            'map_id' => $this->courier_id,
            'type' => 1,
        ];
        $listCount = ModelWithdrawLog::getInstance()->toCount($where);
        $totalPage = ceil($listCount / $listRow);//总页数
        _toCompare(($page > $totalPage && $totalPage > 0), '==', true, 10007);
        $firstRow = ($page - 1) * $listRow;

        $field = 'id,withdraw_number,money,gmt_create';
        $logList = ModelWithdrawLog::getInstance()->toSelect($where,$field,$firstRow,$listRow,'gmt_create desc');
        foreach ($logList as &$value){
            $value['money'] = '-'.$value['money'];
        }
        $data['withdrawLog'] = $logList;
        $data['totalPages'] = $totalPage;
        _returnData($data);

    }
    /*
     * 提现详情
     *  URI： '/courier/withdraw.details'
     */
    public function withdrawDetail()
    {
        $id = $this->_getContentParams('id');
//        $id = 1;
        _toValidate('id', $id, 10009);
        $field = 'money,withdraw_number as withdrawNumber,bank_card_number as cardNumber,state,gmt_create as createTime,gmt_modified as gmtModified';
        $details = ModelWithdrawLog::getInstance()->toFind(['id'=>$id],$field);
        _returnData(['withdrawDetail'=>$details]);
    }
    /*
     * 账户明细
     * URI：/courier/account.details
     *
     */
    public function accountDetails()
    {
        $date = $this->_getContentParams('date');
//        $date = '2018-05';
        $startTime = $date.'-01 00:00:00';//每月开始时间
        $endTime = date('Y-m-d', strtotime("$date +1 month -1 day")).' 23:59:59';  // date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));//每月结束时间

        //当月总收入
        $totalInCome = ModelCashStream::getInstance()->toFind(['map_id'=>$this->courier_id,'map_type'=>1,'is_income'=>0,'gmt_create'=> array('between',[$startTime,$endTime])],'ifnull(sum(money),0.00) as totalInCome');
        //当月总支出
        $totalExpend = ModelCashStream::getInstance()->toFind(['map_id'=>$this->courier_id,'map_type'=>1,'is_income'=>1,'gmt_create'=> array('between',[$startTime,$endTime])],'ifnull(sum(money),0.00) as totalExpend');

        $dateArr = ModelCashStream::getInstance()->toSelect(['map_id'=>$this->courier_id,'map_type'=>1,'gmt_create'=>['between',[$startTime,$endTime]]],"date_format(gmt_create,'%Y-%m-%d') as time",'','','gmt_create desc',"date_format(gmt_create,'%Y%m%d')");
//        echo Db::name('')->getLastSql();
        foreach ($dateArr as $key=> $value){

            $dayStartTime = $value['time'].' 00:00:00';
            $dayEndTime = $value['time'].' 23:59:59';
            //获取每天的收入
            $dayIncome = ModelCashStream::getInstance()->toFind(['map_id'=>$this->courier_id,'map_type'=>1,'is_income'=>0,'gmt_create'=> array('between',[$dayStartTime,$dayEndTime])],"ifnull(sum(money),0.00) as dayInCome");
//            echo Db::name('')->getLastSql();die;
            //每日支出
            $dayExpend = ModelCashStream::getInstance()->toFind(['map_id'=>$this->courier_id,'map_type'=>1,'is_income'=>1,'gmt_create'=> array('between',[$dayStartTime,$dayEndTime])],"ifnull(sum(money),0.00) as dayExpend");
//            dump($dayExpend);
            //每日列表
            $dayList = ModelCashStream::getInstance()->toSelect(['map_id'=>$this->courier_id,'map_type'=>1,'gmt_create'=> array('between',[$dayStartTime,$dayEndTime])],'describe,money,is_income as isIncome,order_number as orderNumber,order_id as orderId');

            $data['mouthList'][$key]['dayIncome'] = $dayIncome['dayInCome'];
            $data['mouthList'][$key]['dayExpend'] = $dayExpend['dayExpend'];
            $data['mouthList'][$key]['week'] = date('N',strtotime($dayEndTime));
            $data['mouthList'][$key]['day'] = date('d',strtotime($dayEndTime)).'日';
            $data['mouthList'][$key]['dayList'] = $dayList;

        }

        $data['totalInCome'] = $totalInCome['totalInCome'];
        $data['totalExpend'] = $totalExpend['totalExpend'];

        _returnData(['accountList'=> $data]);
//        echo Db::name('')->getLastSql();
    }
}
